<template>
  <div class="box">
    <div class="content">
      <div class="title">登录</div>
      <el-form ref="form" :model="form" label-width="80px" class="from">
        <div class="demo-input-suffix">
          <span class="label">用户名:</span>
          <div class="shuru">
            <el-input
              v-model="form.username"
              placeholder="请输入用户名"
              autocomplete="on"
            ></el-input>
          </div>
        </div>
        <div class="demo-input-suffix">
          <span class="label">密码:</span>
          <div class="shuru">
            <el-input
              v-model="form.pass"
              placeholder="请输入密码"
              autocomplete="on"
            ></el-input>
          </div>
        </div>
        <div class="demo-input-suffix">
          <el-button class="denlu" @click="getCompany">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import { login, getUserInfo } from "@/api/index.js";
import { ElMessage } from "element-plus";
export default {
  name: "",
  data() {
    return {
      form: {
        username: 'admin',
        pass: '123456',
      },
    };
  },
  mounted() {
    // this.getCompany();
  },
  methods: {
    getCompany() {
      var that = this;
      login(that.form)
        .then((res) => {
          if (res.code == 1) {
            let token = res.data.token;
            that.setToken(token);
            that.userinfo().then(function () {
              // console.log(333333333);
              ElMessage.success({
                message: "恭喜你，登陆成功",
                type: "success",
              });
              // console.log(44444444);
              that.$router.push("/");
            });
          } else {
            ElMessage.error("登陆失败");
          }
        })
        .catch((error) => {
          console.log(error);
          ElMessage.error("登陆失败");
        });
    },
    setToken(token) {
      // console.log(1111111);
      sessionStorage.setItem("accessToken", token);
    },
   async userinfo() {
      await getUserInfo()
        .then(function (res) {
          if (res.code == 1) {
            //缓存用户信息
            sessionStorage.setItem("userinfo", JSON.stringify(res.data));
            sessionStorage.setItem("username", res.data.info.username);
            // console.log(222222222);
          }
        })
        .catch(function (error) {
          console.log(error);
        });
      return Promise.resolve();
    },
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
.box {
  width: 400px;
  height: 300px;
  border: 1px solid black;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(0, -50%);
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

.box .content {
  margin-top: 40px;
  margin-left: 20px;
  width: 80%;
}
.box .content .from {
  /* border: 1px solid black; */
  text-align: center;
}

.demo-input-suffix {
  display: inline-block;
  width: 100%;
  /* border: 1px solid red; */
  height: 40px;
  margin-top: 30px;
  margin-left: 20px;
}

.demo-input-suffix .label,
.demo-input-suffix .shuru {
  display: inline;
  float: left;
}
.demo-input-suffix .label {
  line-height: 40px;
  margin-right: 10px;
  width: 20%;
}
.demo-input-suffix .shuru {
  width: 60%;
}

.demo-input-suffix .denlu {
  width: 60%;
  /* border: 1px solid red; */
}
</style>>

